#include "contact.pb.h"
#include <iostream>
#include <fstream>
#include <string>

void AddPeopleInfo(contacts2::PeopleInfo* people)
{
    std::cout<<"********新增联系人******:"<<std::endl;
    std::cout<<"输入姓名: ";
    std::string name;
    getline(std::cin,name);
    people->set_name(name);
    std::cout<<"输入年龄: ";
    int age;
    std::cin>>age;
    people->set_age(age);
    std::cin.ignore(256, '\n');//跳过'\n',结束

    for(int i=0;;i++)
    {
        std::cout<<"联系人电话: ";
        std::string _phone;
        getline(std::cin,_phone);
        if(_phone.empty()) break;

        contacts2::PeopleInfo_Phone* phone = people->add_phone();//内类成员
        phone->set_number(_phone);

        std::cout << "请输入该电话类型(1、移动电话   2、固定电话): ";
        int type;
        std::cin>>type;
        std::cin.ignore(256,'\n');//结束输入
        switch(type)
        {
            case 1:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::
                                PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::
                                PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                std::cout<<" input error"<<std::endl;
                break;
        }
    }

    contacts2::Address address;
    std::cout << "请输入联系人家庭地址：";
    std::string home_address;
    getline(std::cin , home_address);
    address.set_home_address(home_address);

    std::cout << "请输入联系人单位地址：";
    std::string unit_address;
    getline(std::cin, unit_address);

    address.set_unit_address(unit_address);
    // Address->Any
    people->mutable_data()->PackFrom(address);

    std::cout << "请选择要添加的其他联系方式(1、qq     2、wechat):";
    int other_contact;
    std::cin>>other_contact;
    std::cin.ignore(256, '\n');

    if(other_contact==1)
    {
        std::cout<<"输入QQ: ";
        std::string qq;
        getline(std::cin, qq);
        people->set_qq(qq);
    }
    else if(other_contact==2)
    {
        std::cout<<"输入wechat: ";
        std::string wechat;
        getline(std::cin, wechat);
        people->set_wechat(wechat);
    }
    else std::cout<<"输入错误！"<<std::endl;

    for (int i = 0;; i++) 
    {
        std::cout << "请输入备注" << i+1 << "标题(只输入回车完成备注新增):";
        std::string remark_key;
        getline(std::cin, remark_key);
        if (remark_key.empty()) 
        {
            break;
        }

        std::cout << "请输入备注" << i+1 << "内容: ";
        std::string remake_value;
        getline(std::cin, remake_value);
        people->mutable_remake()->insert({remark_key, remake_value});
    }


    std::cout << "-----------添加联系⼈成功-----------" << std::endl;


}
int main()
{
    contacts2::Contacts _con;
    std::fstream input("contacts.bin", std::ios::in | std::ios::binary);
    if(!input)//失败
    {
        std::cout<<"contacts.bin find erron,create new file"<<std::endl;
    }
    else if (!_con.ParseFromIstream(&input)) 
    {
        std::cerr << "parse error!" << std::endl;
        input.close();
        return -1;
    }

    AddPeopleInfo(_con.add_contacts());

    // 将通讯录写入本地文件中
    std::fstream output("text.bin", std::ios::out | std::ios::trunc | std::ios::binary);
    if (!_con.SerializeToOstream(&output)) 
    {
        std::cerr << "write error!" << std::endl;
        input.close();
        output.close();
        return -1;
    }
    std::cout << "write success!" << std::endl;
    input.close();
    output.close();
    return 0;

}